/**    
 * @{#} RegisterActivity.java Create on 2014-5-26 下午9:51:05    
 *          
 * @author <a href="mailto:evan0502@qq.com">Evan</a>   
 * @version 1.0    
 */
package com.smiier.skin;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import cn.o.app.net.INetTask;
import cn.o.app.net.NetTaskListener;
import cn.o.app.ui.OAlert;
import cn.o.app.ui.OAlert.OAlertListener;
import cn.o.app.util.OUtil;
import cn.skinapp.R;

import com.evan.common.handler.RequestHandler;
import com.evan.common.handler.callback.HandlerCallback;
import com.evan.common.utils.CacheCommon;
import com.smiier.skin.constant.Constant;
import com.smiier.skin.intercept.RequestTaskIntercept;
import com.smiier.skin.net.AccountGetBanksTask;
import com.smiier.skin.net.AccountGetBanksTask.AccountGetBanksRequest;
import com.smiier.skin.net.AccountGetBanksTask.AccountGetBanksResponse;
import com.smiier.skin.net.entity.Bank;
import com.smiier.skin.ui.BasicActivity;
import com.smiier.skin.util.GlobalSettings;
import com.smiier.skin.utils.CommonUtility;

public class TixianActivity extends BasicActivity {

	private EditText edit_tixianjine_value;
	private TextView text_bank_card_num_value, text_kaihuyinhang_value,
			text_kaihu_xingming_value, text_yue;
	TextView mShenFen;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_tixian);
		init();
		setNavTitle("提现");
		edit_tixianjine_value = (EditText) findViewById(R.id.text_tixianjine_value);
		text_bank_card_num_value = (TextView) findViewById(R.id.text_bank_card_num_value);
		text_kaihuyinhang_value = (TextView) findViewById(R.id.text_kaihuyinhang_value);
		text_kaihu_xingming_value = (TextView) findViewById(R.id.text_kaihu_xingming_value);
		mShenFen = findViewById(R.id.text_kaihu_shenfen_value, TextView.class);
		text_yue = (TextView) findViewById(R.id.text_yue);
		text_yue.setText("账户余额：" + GlobalSettings.sYue + "元");

		ArrayList<CacheCommon> caches = dbUtil.queryCache(getClass()
				.getSimpleName());
		String info = CommonUtility.getDBData(caches);
		if (!CommonUtility.isNull(info)) {
			String[] infos = info.split(Constant.SPLIT);
			text_bank_card_num_value.setText(infos[0]);
			text_kaihuyinhang_value.setText(infos[1]);
			text_kaihu_xingming_value.setText(infos[2]);
		}
		CommonUtility.showKeyboard(edit_tixianjine_value);
	}

	@Override
	protected void onResume() {
		super.onResume();
		AccountGetBanksTask task = new AccountGetBanksTask();
		AccountGetBanksRequest req = new AccountGetBanksRequest();
		req.token = GlobalSettings.sToken;
		task.addListener(new NetTaskListener<AccountGetBanksTask.AccountGetBanksRequest, AccountGetBanksTask.AccountGetBanksResponse>() {

			@Override
			public void onException(
					INetTask<AccountGetBanksRequest, AccountGetBanksResponse> task,
					Exception e) {

			}

			@Override
			public void onComplete(
					INetTask<AccountGetBanksRequest, AccountGetBanksResponse> task,
					AccountGetBanksResponse response) {
				if (response != null && response.ResultCode == 200
						&& response.Res.size() > 0) {
					Bank bank = response.Res.get(0);
					text_bank_card_num_value.setText(bank.Card);
					text_kaihuyinhang_value.setText(bank.Bank);
					text_kaihu_xingming_value.setText(bank.Name);
					mShenFen.setText(bank.IDCard);
				}
			}
		});
		task.setRequest(req);
		add(task);
	}

	public void onClick(View view) {
		super.onClick(view);
		int id = view.getId();
		Intent intent = null;
		if (id == R.id.text_bank_card_num) {
			intent = new Intent(activity, UpdateInfoInputActivity.class);
			intent.putExtra(Constant.IDENTITY_KEY, true);
			intent.putExtra(Constant.IDENTITY_KEY_1,
					InputType.TYPE_CLASS_NUMBER);
			intent.putExtra(Constant.IDENTITY_KEY_NAV_TITLE, "银行卡号");
			startActivityForResult(intent, 1);
		} else if (id == R.id.text_kaihuyinhang) {
			intent = new Intent(activity, CommonDataChooseActivity.class);
			String[] datas = { Constant.URL.REQUEST_BANK_JSON, "开户银行", "Bank",
					CommonDataChooseActivity.TYPE_URL,
					CommonDataChooseActivity.SINGLECHOOSE, "" };
			intent.putExtra(Constant.IDENTITY_KEY, datas);
			intent.putExtra(Constant.IDENTITY_KEY_1, true);
			startActivityForResult(intent, 2);
		} else if (id == R.id.text_kaihu_xingming) {
			intent = new Intent(activity, UpdateInfoInputActivity.class);
			intent.putExtra(Constant.IDENTITY_KEY_NAV_TITLE, "开户姓名");
			intent.putExtra(Constant.IDENTITY_KEY, true);
			startActivityForResult(intent, 3);
		} else if (id == R.id.text_kaihu_shenfen) {
			intent = new Intent(activity, UpdateInfoInputActivity.class);
			intent.putExtra(Constant.IDENTITY_KEY_NAV_TITLE, "身份证");
			intent.putExtra(Constant.IDENTITY_KEY, true);
			startActivityForResult(intent, 4);
		} else if (id == R.id.btn_all_cost) {
			edit_tixianjine_value.setText(GlobalSettings.sYue + "");
		} else if (id == R.id.btn_submit) {
			final String card_num = text_bank_card_num_value.getText()
					.toString();
			final String kaihuyinhang = text_kaihuyinhang_value.getText()
					.toString();
			final String xingming = text_kaihu_xingming_value.getText()
					.toString();
			final String cost = edit_tixianjine_value.getText().toString();
			final String shenfen = mShenFen.getText().toString();
			try {
				Double pay = Double.parseDouble(text_yue.getText().toString());
				if (pay > GlobalSettings.sYue) {
					toast("提现金额大于账户金额");
					return;
				}
			} catch (Exception e) {
				toast("请您输入正确的金额");
			}

			if (GlobalSettings.sYue < 100) {
				toast("提现金额小于100，攒攒钱再来吧~~");
				return;
			}

			if (!CommonUtility.isNull(card_num)
					&& !CommonUtility.isNull(kaihuyinhang)
					&& !CommonUtility.isNull(xingming)
					&& !CommonUtility.isNull(cost)) {
				RequestTaskIntercept.requestIntercept(activity,
						Constant.URL.REQUEST, new RequestHandler(activity,
								new HandlerCallback() {

									@Override
									public void callback(Object o) {
										if (!CommonUtility.isNull(o)) {
											try {
												JSONObject object = (JSONObject) o;
												boolean isInit = object
														.getBoolean(Constant.JSON_PARAM_RES);
												if (isInit) {
													OAlert alert = new OAlert(
															getContext());
													alert.setOK("确认");
													alert.setCancel("取消");
													alert.setTitle("请输入交易密码");
													alert.setContentView(R.layout.view_input_pay_password);
													final EditText edit_pay_password = (EditText) alert
															.findViewById(R.id.edit_pay_password);
													CommonUtility
															.showKeyboard(edit_pay_password);
													alert.show();
													alert.setListener(new OAlertListener() {

														@Override
														public boolean onOK(
																OAlert alert) {
															String pay_password = edit_pay_password
																	.getText()
																	.toString();
															if (!CommonUtility
																	.isNull(pay_password)) {
																HashMap<String, Object> params = new HashMap<String, Object>();
																params.put(
																		Constant.PARAM_API,
																		"Account.Draw");
																params.put(
																		Constant.PARAM_TOKEN,
																		GlobalSettings.sToken);
																params.put(
																		"card",
																		card_num);
																params.put(
																		"bank",
																		kaihuyinhang);
																params.put(
																		"name",
																		xingming);
																params.put(
																		"cost",
																		cost);
																params.put(
																		"PayPwd",
																		OUtil.md5(pay_password));
																StringBuilder sb = new StringBuilder();
																sb.append(
																		card_num)
																		.append(Constant.SPLIT)
																		.append(kaihuyinhang)
																		.append(Constant.SPLIT)
																		.append(xingming);
																dbUtil.saveOrUpdate(
																		getClass()
																				.getSimpleName(),
																		sb);
																RequestTaskIntercept
																		.requestIntercept(
																				activity,
																				Constant.URL.REQUEST,
																				new RequestHandler(
																						activity,
																						new HandlerCallback() {

																							@Override
																							public void callback(
																									Object o) {
																								if (!CommonUtility
																										.isNull(o)) {
																									JSONObject object = (JSONObject) o;
																									if (CommonUtility
																											.response200(object)) {
																										CommonUtility
																												.toast(activity,
																														"提现请求成功");
																										Intent intent = new Intent(
																												Constant.RECEIVER_ACCOUNT_CHONGZHI);
																										sendBroadcast(intent);
																									} else {
																										try {
																											CommonUtility
																													.toast(activity,
																															object.getString("ResultMessage"));
																										} catch (Exception e1) {
																											e1.printStackTrace();
																										}
																									}
																								}
																							}
																						}),
																				params);
															} else {
																CommonUtility
																		.toast(activity,
																				"请输入交易密码");
																return true;
															}
															return false;
														}

														@Override
														public boolean onCancel(
																OAlert alert) {
															return false;
														}
													});
												} else {
													OAlert alert = new OAlert(
															getContext());
													alert.setOK("确认");
													alert.setCancel("取消");
													alert.setTitle(R.string.s_init_paypassword);
													alert.show();
													alert.setListener(new OAlertListener() {

														@Override
														public boolean onOK(
																OAlert alert) {
															Intent intent = null;
															try {
																if (CommonUtility
																		.isNull(GlobalSettings.sUser.Mobile)) {
																	intent = new Intent(
																			activity,
																			RegisterFirstStepActivity.class);
																	intent.putExtra(
																			Constant.IDENTITY_KEY_1,
																			Constant.TYPE_UPDATE_MOBILE);
																	startActivity(intent);
																} else {
																	CommonUtility
																			.confirmSendCode(
																					activity,
																					GlobalSettings.sUser.Mobile,
																					Constant.TYPE_RESET_PAY_PASSWORD);
																}
															} catch (Exception e) {
																e.printStackTrace();
															}
															return false;
														}

														@Override
														public boolean onCancel(
																OAlert alert) {
															return false;
														}
													});
												}
											} catch (Exception e) {
												e.printStackTrace();
											}
										}
									}
								}), CommonUtility.params(new String[] {
								Constant.PARAM_API, Constant.PARAM_TOKEN },
								new String[] { "Account.CheckInitPayPwd",
										GlobalSettings.sToken }));
			} else {
				CommonUtility.tip(activity, "请确认您是否完整填写信息");
			}
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (!CommonUtility.isNull(data)) {
			String value = data.getStringExtra(Constant.IDENTITY_KEY);
			switch (requestCode) {
			case 1:
				text_bank_card_num_value.setText(value);
				break;
			case 2:
				text_kaihuyinhang_value.setText(value);
				break;
			case 3:
				text_kaihu_xingming_value.setText(value);
				break;
			case 4:
				mShenFen.setText(value);
				break;
			}
		}
	}

	/*
	 * @Override public void onResume() { super.onResume();
	 * MobclickAgent.onResume(getContext()); }
	 * 
	 * @Override public void onPause() { super.onPause();
	 * MobclickAgent.onPause(getContext()); }
	 */
}
